$(function () {
    var loading = weui.loading('loading', {
        className: 'custom-classname'
    });
    var gid = Common.fn.getUrlParam('gid'),
        result = Model.goods.salerGoodsDetail({
            goodsId: gid
        }),
        state = result.data.goods.state,
        sendState = result.data.goods.postage,
        endTime = result.data.goods.endTime,
        endYear = parseInt(endTime.substr(0, 4)),
        endMonth = parseInt(endTime.substr(5, 2)),
        endDay = parseInt(endTime.substr(8, 2)),
        endHour = parseInt(endTime.substr(11, 2)),
        endMinute = parseInt(endTime.substr(14, 2)),
        endSecond = parseInt(endTime.substr(17, 2)),
        targetEndTime = (new Date(endYear, endMonth - 1, endDay, endHour, endMinute, endSecond)).getTime(),
        info = result.data.goods.content,
        img = result.data.images[0].image;
    Model.wechat.weShare(gid, info, img);
    if (!sessionStorage.getItem('hmdj_stopError')) {
        sessionStorage.setItem('hmdj_stopError', true);
    } else {
        if (result.code) {
            Common.fn.formatOutput(result);
            loading.hide();
            return false;
        }
    }
    // 显示商品图片
    for (var i = 0; i < result.data.images.length; i++) {
        var item = '<img class="goods-images-item" src="' + result.data.images[i].image + '">';
        $('.goods-detail-thumb-item').eq(i).append(item);
    }
    // 关注按钮改变
    if (result.data.goods.attention == true) {
        // 已关注
        $('.attention-btn').text('已关注');
        $('.attention-btn').attr('data-state', 1);
    } else {
        $('.attention-btn').text('关注');
        $('.attention-btn').attr('data-state', 0);
    }
    // 设置其他信息
    $('.portrait-img').attr('src', result.data.bussiness.photo);
    $('.vendor-level').text('v' + result.data.bussiness.grade);
    $('h3').text(result.data.bussiness.nickname);
    $('.score').text(result.data.bussiness.credits);
    $('.bond').text(result.data.bussiness.bond);
    result.data.bussiness.auth == true ? $('.auth').text('已认证') : $('.auth').text('未认证');
    $('.goods-detail').text(result.data.goods.content);
    $('.topPrice').text(result.data.goods.topPrice);
    $('.order-count').text(result.data.goods.join_count);
    $('.vendor-date').text(result.data.goods.createTime);
    $('.praiseNumber').text(result.data.goods.praiseNumber);
    $('.attention-btn').attr('data-id', result.data.bussiness.businessId);
    $('.tuiguang').attr('data-id', result.data.goods.goodsId);
    $('.tuiguang').attr('data-isShare', result.data.goods.share);
    $('.portrait-block').attr('href', 'shop_detail.html?id=' + result.data.bussiness.businessId);
    sendState ? $('.sendState').text('包邮') : $('.sendState').text('不包邮');
    // 判断商品是否可以被分享
    if(parseInt(result.data.goods.state) == 1) {
        // 可以被分享
        $('.share-btn').attr('data-isShare', true);
    }else {
        // 不可以被分享
        $('.share-btn').attr('data-isShare', false);
    }
    // 判断商品是否被点赞
    if (result.data.goods.praise) {
        // 已点赞
        $('.like-icon').attr('src', '../img/icon/like_1.png');
    }
    // 抢购倒计时
    var time = setInterval(function () {
        var currentTime = Date.now(),
            offsetTime = targetEndTime - currentTime;
        if (offsetTime < 0) {
            clearInterval(time);
            return false;
        }
        var offsetHour = Math.floor(offsetTime / (3600 * 1e3)),
            remainTime = offsetTime - (offsetHour * 3600 * 1e3),
            offsetMinute = Math.floor(remainTime / (60 * 1e3)),
            remainTime2 = remainTime - (offsetMinute * 60 * 1e3),
            offsetSecond = Math.floor(remainTime2 / 1e3);
        $('.offset-hour').text(offsetHour);
        $('.offset-minute').text(offsetMinute);
        $('.offset-second').text(offsetSecond);
        if (offsetHour == 0 && offsetMinute == 0 && offsetSecond == 0) {
            clearInterval(time);
        }
    }, 1000);
    // 参拍用户
    $('.patron-number').text(result.data.orders.length);
    for (var i = 0; i < result.data.orders.length; i++) {
        var item = '<div class="weui-panel weui-panel_access patron-user"><a href = "javascript:void(0);" class="weui-media-box weui-media-box_appmsg patron-a"><div class="weui-media-box__hd patron-user"><div class="portrait-block"><label class="user-level">v' + result.data.orders[i].grade + '</label><img class="portrait-img" src="' + result.data.orders[i].photo + '" width="70" height="70"></div></div><div class="weui-media-box__bd"><div class="weui-cell"><div class="weui-cell__bd patron-username"><p>' + result.data.orders[i].nickname + '</p></div></div><div class="weui-cell"><p class="weui-media-box__desc"><p class="weui-cell__bd patron-price">￥' + result.data.orders[i].price + '</p><div class="weui-cell__ft"><p class="text-1-2 text-right">' + result.data.orders[i].time + '</p></div></div></div></a></div>';
        $('.patron-items').append(item);
    }
    loading.hide();
});

// 关注按钮
$(document).on('click', '.attention-btn', function () {
    var id = $(this).attr('data-id'),
        state = $(this).attr('data-state');
    if (state == 1) {
        // 已关注
        weui.confirm(
            "真的要取消关注吗？",
            function () {
                // 取消关注
                result = Model.business.attendBusiness({
                    businessId: id
                });
                if (result.code) {
                    alert(result.message);
                    location.reload();
                    return false;
                }
                location.reload();
            },
            function () { }
        );
    } else {
        // 关注店铺
        var loading = weui.loading('loading', {
            className: 'custom-classname'
        });
        result = Model.business.attendBusiness({
            businessId: id
        });
        if (result.code) {
            Common.fn.formatOutput(result);
            loading.hide();
            return false;
        }
        loading.hide();
        location.reload();
    }
});

// 点赞按钮
$(document).on('click', '.like-icon', function () {
    var gid = Common.fn.getUrlParam('gid'),
        result = Model.goods.collectGoods({
            goodId: gid
        });
    if (result.code) {
        Common.fn.formatOutput(result);
        return false;
    }
    location.reload();
});

// 立即推广
$(document).on('click', '.tuiguang', function () {
    var id = $(this).attr('data-id'),
        isShare = $(this).attr('data-isShare'),
        result = Model.goods.spread({
            goodsId: id
        });
    if (isShare == 'false') {
        weui.alert('请先分享再进行推广');
        return false;
    }
    if (result.code) {
        Common.fn.formatOutput(result);
        return false;
    }
    weui.alert(result.data, function () {
        location.reload();
    });
});

// 分享
$(document).on('click', '.share-btn', function () {
    var isShare = $(this).attr('data-isShare');
    if(isShare == 'true') {
        weui.alert('请点击右上角菜单 -> 发送给朋友或分享到朋友圈 进行分享');
    }else {
        weui.alert('只有上架的商品才可以被分享');
    }
    
});

// 点击头像跳转店铺
/* $(document).on('click', '.portrait-block', function () {
    var id = $(this).attr('data-id');
    location.href = 'shop_detail.html?id=' + id;
}); */

// 点击图片放大
$(document).on('click', '.goods-images-item', function () {
    var url = $(this).attr('src');
    var gallery = weui.gallery(url, {
        className: 'custom-classname',
        onDelete: function () {
            gallery.hide(function () {
                console.log('`gallery` has been hidden');
            });
        }
    });
});